/*******************************************************************************

This code file produces Panel A of Figure A24, "421-a Participation Under Alternative Tax Incentives and Inclusionary Shares."

*******************************************************************************/

*** Manage settings

	run "$dir/code/modules/settings.do"

********************************************************************************
* Citywide supply curve
********************************************************************************
		
*** Bootstrap estimation of supply curve

	clear
	tempfile final
	set obs 1
	gen scale = .
	gen pr = .
	save "$data/raw/boot_curve.dta", replace
	
	forvalues b = 1(1)1001 {
	
		di `b'
		
		clear
		tempfile record
		quietly set obs 1
		quietly gen scale = .
		quietly gen pr = .
		save `record', replace
		
		use "$data/clean/cleaned_data.dta", clear
		set seed 1234
		bsample, cluster(nta) idcluster(nta_boot)
		
		quietly logit inclusionary_onsite dtaxrate_onsite $controls_lot $controls_block i.borough i.yearpermit, cl(nta_boot)
		mat b = e(b)
		local coef_dtaxrate = b[1,1]
		
		quietly predict xb, xb
		
		forvalues x = 0(0.05)1.5 {
			
			quietly gen pr = exp(xb+`coef_dtaxrate'*(`x'-dtaxrate_onsite))/(1+exp(xb+`coef_dtaxrate'*(`x'-dtaxrate_onsite)))
		
			local tmp0 = `x'
			quietly summ pr [aw=unitsres]
			local tmp1 = r(mean)
			
			preserve
			clear
			
			quietly set obs 1
			quietly gen scale = `tmp0'
			quietly gen pr = `tmp1'
			
			quietly append using `record'
			quietly save `record', replace
			
			restore
			
			drop pr
			
		}
		
		use `record', clear
		
		gen b = `b'
		
		quietly append using "$data/raw/boot_curve.dta"
		quietly save "$data/raw/boot_curve.dta", replace
		
	}
	

*** Store actuals

	use "$data/clean/cleaned_data.dta", clear
	quietly logit inclusionary_onsite dtaxrate_onsite $controls_lot $controls_block i.borough i.yearpermit, cl(nta)
	gen samp = e(sample)
	
	summ dtaxrate_onsite if samp==1 [aw=unitsres]
	local avgtaxrate = r(mean)
	summ inclusionary_onsite if samp==1 [aw=unitsres]
	local sh_incl_onsite = r(mean)
	
*** Chernozhukov et al procedure for simultaneous confidence intervals

	use "$data/raw/boot_curve.dta",clear
	
	bys scale: egen pr_mean = mean(pr)
	bys scale: egen sd = sd(pr)
	gen t = abs(pr - pr_mean)/sd
	bys b: egen max_t = max(t)
	
	summ max_t, d
	local p95 = r(p95)
	
	gen lo = pr_mean - `p95'*sd
	gen hi = pr_mean + `p95'*sd
	
	replace hi = min(1,hi)
	replace lo = max(0,lo)

*** Produce graph

	sort scale
	
	tw (line pr_mean scale if b==2, lcolor(edkblue)) || ///
		(rline lo hi scale if b==2, lcolor(ltblue)), ///
		yline(`sh_incl_onsite', lpattern(dash) lcolor(gs9)) ///
		xline(`avgtaxrate', lpattern(dash) lcolor(gs9)) ///
		graphregion(color(white)) ylabel(, nogrid) ///
		legend(order(1 "Bootstrap Mean" 2 "95% Confidence Band")) ///
		ytitle("Citywide 421-a Participation Rate") xtitle("421-a Incentive (as a Fraction of Building Value)")
		
	graph export "$figs/citywide_supply_curve_nofe.pdf", replace
	graph export "$figs_overleaf/citywide_supply_curve_nofe.pdf", replace
